//author:jxx

import { data } from "jquery";
import store from "@/store/index";
//此处是对表单的方法，组件，权限操作按钮等进行任意扩展(方法扩展可参照SellOrder.js)
let singList=[]

let extension = {
  components: {//动态扩充组件或组件路径
    //表单header、content、footer对应位置扩充的组件
    gridHeader: '',//{ template: "<div>扩展组xx件</div>" },
    gridBody: '',
    gridFooter: '',
    //弹出框(修改、编辑、查看)header、content、footer对应位置扩充的组件
    modelHeader: '',
    modelBody: '',
    modelFooter: ''
  },
  buttons: { view: [], box: [], detail: [] },//扩展的按钮
  methods: {//事件扩展
    getFormOption (field) {
      let option;
      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field == field) {
            option = item;
          }
        })
      })
      return option;
    },
    getValue(keyToFind){
      let value = ""
      singList.forEach((item)=>{
        if(item.key==keyToFind)
        value=item.value
      })
      return value
    },
    onInit () {
      //新建窗口的宽和高
      this.boxOptions.width = 1000;
      this.boxOptions.height = 500;
      ////;
      this.textInline = true;
      // //;
      // //;
      this.http.post('/api/Dictionary/GetVueDictionary', ['Signs']).then(res => {
        singList=res[0].data
    })
      this.rowKey = "typeCode";
      this.maxBtnLength = 5;
      let _userInfo = store.getters.getUserInfo();
      let parentIDoption = this.getFormOption("parentID");
      let attributesoption = this.getFormOption("attributes");
      let typeNameoption = this.getFormOption("typeName");
      let typeCodeoption = this.getFormOption("typeCode");
      let isMessage = this.getFormOption("isMessage");
      console.log('isMessage',isMessage.value);
      //let merchantCodeoption= this.getFormOption("roleId");
      let spare10 = this.getFormOption("smsNote");
      let smsId = this.getFormOption("smsName");
      smsId.onChange = (val, option) => {

        spare10.hidden = false;
        let data = option.data.filter(item => item.key == val);
        if (data.length > 0) { this.editFormFields[spare10.field] = data[0].remark; } else { spare10.hidden = true }
      }
      parentIDoption.changeOnSelect = true;
      // typeNameoption.onInput = (val) => {
      //   this.editFormFields[typeCodeoption.field] = this.base.chineseToPinYin(val)
      // }
      if (_userInfo.roleID == 1) {
        parentIDoption.required = false;
        attributesoption.required = false;
        // merchantCodeoption.hidden=false;
      } else {
        parentIDoption.required = true;
        attributesoption.required = true;
        //merchantCodeoption.hidden=true;
      }
      this.columns.push({
        title: '操作',
        fixed: 'right',
        width: 140,
        render: (h, { row, column, index }) => {
          if (row.parentID != 0 || _userInfo.roleID == 1) {
            return h(
              "div", { style: {} }, [
              h(
                "a", {
                props: {},
                style: {},
                on: {
                  click: (e) => {
                    this.linkData(row);
                  }
                }
              },
                "编辑"
              ),
            ])
          }

        }
      })
      this.columns.forEach(item => {
        if (_userInfo.roleID != 1) {
          if (item.field == "black_ico" || item.field == "color_ico" || item.field == "uiType") {
            item.hidden = true;
          }
        } else {
          if (item.field == "black_ico" || item.field == "color_ico" || item.field == "uiType") {
            item.hidden = false;
          }
        }
      })
      //this.$store.getters.data().reportPrice = this;
      // this.editFormFileds.parentID = [];
      //     var selectOption = this.getFormOption('parentID');
      //     //selectOption.type = "cascader";
      //     //selectOption.changeOnSelect = true
      //     //初始一个空的数据源(必须)!
      //     //selectOption.data=[];
      //     selectOption.onChange = (val, item) => {  
      //     let productname="";
      //     let productnames="";
      //     let prodctcode="";
      //     let ces = 0;
      //     for(let i=item.length-1;i>=0;i--){
      //         ces++;
      //       if(i==0){productname+=item[i].label;prodctcode+=item[i].value};
      //       if(i==item.length-1){productnames+=item[i].label;prodctcode+=item[i].value};
      //     }
      //     //if(this.editFormFileds.parentName==''){
      //     // if(productname!=null&&productnames!=null){
      //     //     this.editFormFileds.parentName=productname+("/")+productnames;
      //     // }else if(productname!=null){
      //     //     this.editFormFileds.parentName=productname;
      //     // }
      //     if(ces==1){
      //         this.editFormFileds.parentName=productname;
      //     }else if(ces > 1){
      //         this.editFormFileds.parentName=productname+("/")+productnames;
      //     }
      //       //this.editFormFields.parentName = Productnames;
      //       //this.editFormFileds.typeCode=prodctcode;
      //    // }
      //       //this.getcolumn(val);
      //       //this.$set(selectOption, "disabled", true);
      //       }
    },

    direct () {
      let isMessage = this.getFormOption("isMessage");
      let smsId = this.getFormOption("smsName");
      let smsMode = this.getFormOption("smsModeName");
      let signCode = this.getFormOption("signCode");//短信签名
      let contactPerson = this.getFormOption("contactPerson");
      let contactPhone = this.getFormOption("contactPhone");

      isMessage.hidden = false;
      if (this.editFormFields[isMessage.field] == 1) {
        smsId.required = true;
        smsMode.required = true;
        signCode.required = true;
      }
      smsId.hidden = false;
      smsMode.hidden = false;
      signCode.hidden = false;
      contactPerson.hidden = false;
      contactPhone.hidden = false;

    },
    loadTreeChildren (tree, treeNode, resolve) { //加载子节点
      let url = `api/producttypes/getTreeTableChildrenData?typeId=${tree.id}`;
      this.http.post(url, {}).then(result => {
        resolve(result.rows)
      })
    },

    searchBefore (params) {//判断加载根节点或子节点
      //没有查询条件，默认查询返回所有根节点数据
      if (!params.wheres.length) {
        params.value = 1;
      }
      return true;
    },
    addAfter (result) {//新建保存后result返回的状态及表单对象
      this.$router.go(0)
      return true;
    },
    updateAfter (result) {//编辑保存后result返回的状态及表单对象
      this.$router.go(0)
      return true;
    },
    delAfter (result) {//查询界面的表删除后
      this.$router.go(0)
      return true;
    },
    rowClick ({ row, column, event }) { //查询界面table点击行选中当前行

      this.$refs.table.$refs.table.toggleRowSelection(row);
      return true;
    },
    async modelOpenBeforeAsync (row) {
      let smsId = this.getFormOption("smsName");
      let smsMode = this.getFormOption("smsModeName");
      let signCode = this.getFormOption("signCode");
      let spare10 = this.getFormOption("smsNote");
      let isMessage = this.getFormOption("isMessage");

      smsId.hidden = false;
      smsMode.hidden = false;
      signCode.hidden = false;
      spare10.hidden = true;
      smsId.required = false;
      smsMode.required = false;
      signCode.required = false;

      isMessage.onChange = (val, option) => {

        console.log(val);
        console.log(option);
        if (val == 1) {

          smsId.hidden = false;
          smsMode.hidden = false;
          signCode.hidden = false;
          smsId.required = true;
          smsMode.required = true;
          signCode.required = true;
        } else {

          this.editFormFields[signCode.field] = "";
          this.editFormFields[smsId.field] = "";
          this.editFormFields[smsMode.field] = "";
          smsId.hidden = true;
          smsMode.hidden = true;
          signCode.hidden = true;
          spare10.hidden = true;
          smsId.required = false;
          smsMode.required = false;
          signCode.required = false;
        }
      }


      let _userInfo = store.getters.getUserInfo();
      let isEdit = this.currentAction != this.const.ADD;
      console.log(isEdit)
      let isEDIT = this.currentAction == this.const.EDIT;
      // if(isEDIT){
      //     var Code = this.getFormOption('attributes');
      //     this.$set(Code, "disabled", true)
      //     var Set = this.getFormOption('parentName');
      //     this.$set(Set,"disabled",true)
      // }
      //设置让父级类型显示在添加或新建
      let parentIDOption = this.getFormOption("parentID");
      //let roleIdOption = this.getFormOption("roleId");
      let parentNameOption = this.getFormOption("parentName");
      let black_icoOption = this.getFormOption("black_ico");
      let color_icoOption = this.getFormOption("color_ico");
      let uiTypeOption = this.getFormOption("uiType");
      let sortOption = this.getFormOption("sort");
      if (!isEDIT) {
        console.log(11)
        this.$set(parentIDOption, "hidden", false)
      } else {
        console.log(22, row.isMessage)
        if (row.isMessage) {    //发送短信
          smsId.hidden = false;
          smsMode.hidden = false;
          signCode.hidden = false;
          smsId.required = true;
          smsMode.required = true;
          signCode.required = true;
        } else {
          this.editFormFields[signCode.field] = "";
          this.editFormFields[smsId.field] = "";
          this.editFormFields[smsMode.field] = "";
          smsId.hidden = true;
          smsMode.hidden = true;
          signCode.hidden = true;
          spare10.hidden = true;
          smsId.required = false;
          smsMode.required = false;
          signCode.required = false;
        }
        this.$set(parentIDOption, "hidden", true)
      }
      if (isEDIT) {
        console.log(33)

        this.$set(parentNameOption, "hidden", false)

      } else {
        console.log(44)
        if (row.isMessage) {    //发送短信
          smsId.hidden = false;
          smsMode.hidden = false;
          signCode.hidden = false;
          smsId.required = true;
          smsMode.required = true;
          signCode.required = true;
        } else {
          this.editFormFields[signCode.field] = "";
          this.editFormFields[smsId.field] = "";
          this.editFormFields[smsMode.field] = "";
          smsId.hidden = true;
          smsMode.hidden = true;
          signCode.hidden = true;
          spare10.hidden = true;
          smsId.required = false;
          smsMode.required = false;
          signCode.required = false;
        }
        this.$set(parentNameOption, "hidden", true)
      }
      //设置将产品名称转换成编码

      //如果为编辑状态，父级类型不可修改
      this.editFormOptions.forEach(item => {
        item.forEach(x => {
          //如果是编辑,库存设置为只读
          if (x.field == "parentName") {
            this.$set(x, "disabled", isEDIT)
          }
          if (x.field == "attributes") {
            this.$set(x, "disabled", isEDIT)
          }
        })
      })
      if (!isEdit && _userInfo.roleID == 1) {
        console.log(55)

        black_icoOption.hidden = false;
        color_icoOption.hidden = false;
        uiTypeOption.hidden = false;
        sortOption.hidden = false;
        return true;

      } else if (!isEdit && _userInfo.roleID != 1) {
        console.log(66)

        black_icoOption.hidden = true;
        color_icoOption.hidden = true;
        uiTypeOption.hidden = true;
        sortOption.hidden = true;
        return true;
      } else if (isEdit && row.parentID == 0 && _userInfo.roleID != 1) {
        console.log(77)

        this.$message.error("您没有该权限！")
        return false;
      } else if (isEdit && row.parentID == 0 && _userInfo.roleID == 1) {
        console.log(88)

        //roleIdOption.hidden=false;
        black_icoOption.hidden = false;
        color_icoOption.hidden = false;
        uiTypeOption.hidden = false;
        sortOption.hidden = false;
        return true;
      } else if (isEdit && row.parentID != 0) {
        console.log(99)

        // roleIdOption.hidden=true;
        black_icoOption.hidden = true;
        color_icoOption.hidden = true;
        uiTypeOption.hidden = true;
        sortOption.hidden = true;
        return true;
      } else {
        this.$message.error("您没有该权限！")
      }
      return true;

    },
    modelOpenAfter (row) {
      let isEDIT = this.currentAction == this.const.EDIT;
      if (!isEDIT) {
        this.editFormFields.isAddCart = "1";
        var selectOption = this.getFormOption('typeName');
        selectOption.onInput = (val) => {
          this.editFormFields.typeCode = this.base.chineseToPinYin(val);
        }
        this.editFormFields.isMessage = 0
        this.editFormFields.isPickup = 0
        this.editFormFields.isDelivery = 0
      }
    },
    addBefore (formData) { //新建保存前formData为对象，包括明细表
      return this.veriFromTable(formData)
    },
    updateBefore (formData) {
      delete formData.mainData.merchantCode
      delete formData.mainData.merchantName
      delete formData.mainData.attributes
      return true;
    },
    // 保存前编辑 
    veriFromTable (formData) {
      console.log("保存前编辑", formData);
      let parentID = this.getFormOption("parentID")//父级类型
      if (parentID.required) {
        if (!formData.mainData.parentID || formData.mainData.parentID == "") {
          this.$message.error("请选择父级类型！")
          return false
        }
      }
      let attributes = this.getFormOption("attributes")//属性
      console.log("保存前编辑", attributes, formData);
      if (attributes.required) {
        if (!formData.mainData.attributes || formData.mainData.attributes == "") {
          this.$message.error("请选择属性！")
          return false
        }
      }
      formData.mainData.signName=this.getValue(formData.mainData.signCode)
      return true
    },
    delBefore (ids, rows) { //查询界面的表删除前 ids为删除的id数组,rows删除的行
      let _userInfo = store.getters.getUserInfo();
      // console.log("删除", _userInfo, ids, rows);
      for (let index = 0; index < rows.length; index++) {
        const element = rows[index];
        if (element.parentID == 0 && _userInfo.roleID != 1) {
          this.$message.error("您无权删除一级类型" + element.typeName)
          break
        } else {
          // console.log(rows);
          let typeCode = rows.map(i => i.typeCode)
          let params = {
            order: "desc",
            page: 1,
            rows: 30,
            sort: "createDate",
            wheres: `[{\"name\":\"type\",\"value\":\"${typeCode.join()}\",\"displayType\":\"like\"}]`
          }
          this.http.post('/api/Products/getPageData', params).then(res => {
            if (res.rows.length != 0) {
              this.$Message.error('当前产品类型已被产品占用，暂时不能删除');
            } else {
              this.$Modal.confirm({
                title: "删除警告!",
                content:
                  '<p style="color: red;font-weight: bold;letter-spacing: 3px;">确认要删除选择的数据吗?</p>',
                onOk: () => {
                  this.http.post('api/producttypes/del', ids).then(res => {
                    this.$Message.success('删除成功');
                    this.refresh()
                    this.$router.go(0)
                  })
                }
              });
            }
          })
          // return true;
        }
      }
    },
  }
};
export default extension;